//
//  SubscriptionFlowLayout.m
//  New_Runway
//
//  Created by NewLife on 2018/3/19.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "SubscriptionFlowLayout.h"

@implementation SubscriptionFlowLayout
{
    CGFloat margin;
}

- (void)prepareLayout {
    
    [super prepareLayout];
    
    margin = 14.5;
}

- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    
    NSArray<UICollectionViewLayoutAttributes *> *arr = [super layoutAttributesForElementsInRect:rect];
    
    NSMutableArray<UICollectionViewLayoutAttributes *> *arrM = [NSMutableArray arrayWithCapacity:arr.count];
    
    for (NSInteger i = 0; i < arr.count; i++) {
        
        UICollectionViewLayoutAttributes *layoutAtt = arr[i];
        
        if (layoutAtt.representedElementKind == UICollectionElementKindSectionHeader ||
            layoutAtt.representedElementKind == UICollectionElementKindSectionFooter) {
            [arrM addObject:layoutAtt];
            continue;
        }
        
        if (i == 0) {//第一个不改变位置
            
            layoutAtt.frame = CGRectMake(15.5, layoutAtt.frame.origin.y, layoutAtt.frame.size.width, layoutAtt.frame.size.height);
            [arrM addObject:layoutAtt];
        } else {
            
            UICollectionViewLayoutAttributes *lastLayoutAtt = arr[i - 1];
            
            if (lastLayoutAtt.frame.origin.y == layoutAtt.frame.origin.y) {//同一行
                
                layoutAtt.frame = CGRectMake(lastLayoutAtt.frame.origin.x + margin + lastLayoutAtt.size.width, layoutAtt.frame.origin.y, layoutAtt.size.width, layoutAtt.size.height);
            } else {
                
                layoutAtt.frame = CGRectMake(15.5, layoutAtt.frame.origin.y, layoutAtt.frame.size.width, layoutAtt.frame.size.height);
            }
        }
        
        [arrM addObject:layoutAtt];
    }
    return arrM.copy;
}

@end
